package net.lzzy.practices.services;

import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.repositories.CourseRepository;
import net.lzzy.practices.utils.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CourseServiceImpl implements CourseService {
    private CourseRepository repository;

    @Autowired
    public CourseServiceImpl(CourseRepository repository){
        this.repository = repository;
    }

    @Override
    public List<Course> get() {
        return null;
    }

    @Override
    public List<Course> get(boolean open) {
        return repository.findCoursesByOpenEqualsOrderByUpdateTimeDesc(open);
    }

    @Override
    public List<Course> get(int page, int size) {
        return repository.findAllByOrderByUpdateTimeDesc(PageRequest.of(page, size));
    }

    @Override
    public List<Course> get(boolean open, int page, int size) {
        return repository.findCoursesByOpenEqualsOrderByUpdateTimeDesc(PageRequest.of(page, size),open);
    }

    @Override
    public List<Course> getTeachersCourse(UUID teacherId) {
        return repository.findCoursesByTeacherIdEqualsOrderByCreateTimeDesc(teacherId);
    }

    @Override
    public List<Course> getStudentsCourse(UUID studentId) {
        User student = new User();
        student.setId(studentId);
        return repository.findCoursesByStudentsContainsAndOpenOrderByUpdateTimeDesc(student,true);
    }

    @Override
    public Integer count() {
        return repository.countAllBy();
    }

    @Override
    public Integer count(boolean open) {
        return repository.countCourseByOpenEquals(open);
    }

    @Override
    public Course getById(UUID id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public Course add(Course course) {
        return repository.save(course);
    }

    @Override
    public Course update(Course course, String token) {
        UUID teacherId = course.getTeacher().getId();
        if (AuthUtils.invalid(teacherId,token)){
            return null;
        }
        course.setUpdateTime(new Date());
        return repository.save(course);
    }

    @Override
    public Course openCloseCourse(UUID id, boolean open, String token) {
        Course course = repository.findById(id).orElse(null);
        if (course != null){
            course.setOpen(open);
            return update(course, token);
        }
        return null;
    }

    @Override
    public Messages remove(UUID id, String token) {
        Course course = repository.findById(id).orElse(null);
        if (course == null){
            return Messages.WRONG_ID;
        }
        if (AuthUtils.invalid(course.getTeacher().getId(),token)){
            return Messages.NO_PERMISSION;
        }
        if (course.getChapters().size() > 0 || course.getStudents().size() > 0 || course.getApplicants().size() > 0){
            return Messages.RELATED_DATA;
        }
        repository.delete(course);
        return Messages.SUCCESS;
    }

    @Override
    public List<Course> page() {
        return null;
    }
}
